package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.manager.service.VerificationCodeService;
import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.LoginVo;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: indexController
 * @Package: com.atguigu.spzx.manager.controller
 * @Description 解决用户进入首页等问题的控制层
 * @Author 孤妤
 * @Date: 2023/12/3 16:10
 */
@RestController
@RequestMapping("admin/system/index")
@Tag(name = "登录管理")
public class IndexController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private VerificationCodeService verificationCodeService;
    @PostMapping(value = "/login")
    @Operation(summary = "登录功能")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto) ;
        return Result.build(loginVo , ResultCodeEnum.SUCCESS) ;
    }
    @GetMapping("generateValidateCode")
    @Operation(summary = "获取验证码")
    public Result<ValidateCodeVo> getValidateCode(){
        ValidateCodeVo validateCode = verificationCodeService.getValidateCode();
        return Result.build(validateCode,ResultCodeEnum.SUCCESS);
    }
    @GetMapping(value = "/getUserInfo")
    @Operation(summary = "获取用户信息")
    public Result<SysUser> getUserInfo(@RequestHeader(name = "token") String token) {
        SysUser sysUser = sysUserService.getUserInfo(token) ;
        return Result.build(sysUser , ResultCodeEnum.SUCCESS) ;
    }
    @GetMapping(value = "/logout")
    @Operation(summary = "退出登陆")
    public Result logout(@RequestHeader(value = "token") String token) {
        sysUserService.logout(token) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }
}
